/**
 *ChessBoardCreator.java
 */

package chess;

import java.awt.*;
import javax.swing.*;
 
public class ChessBoardCreator extends JPanel{
    public SquareOnBoard[][] sqrs;
    
    /** Creates a new instance of ChessBoardCreator */
    public ChessBoardCreator(){
        setSize(800,700);
        setLayout(new GridLayout(8,8));
        sqrs = new SquareOnBoard[8][8];
        createSquares();
    }
    
    public ChessBoardCreator(int h, int w){
        setSize(h,w);
        setLayout(new GridLayout(8,8));
        sqrs = new SquareOnBoard[8][8];
        createSquares();
    }
    
    private void createSquares(){
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){
                SquareOnBoard panel = new SquareOnBoard(""+i+""+j);
                panel.setSquareColor(getColor(i,j));
                panel.addMouseListener(new MouseHandeling());
                add(panel);
                sqrs[i][j] = panel;
            }
        }
    }
    
    private Color getColor(int x, int y){
        if((x+y)%2 == 0)
            return Color.WHITE;
        else
            return Color.DARK_GRAY;
    }
    
    //TODO i main na gini xehoristi Class H method pou apla tha kalite apo to engine kai meta tha kalounde i endoles.
    public ChessBoardCreator setPieces(){
        ChessBoardCreator t = new ChessBoardCreator();
        //add Pawns to Chess Board.
        for(int i=0; i<8; i++){
            sqrs[1][i].addPiece(new Pawn('w',false));
            Piece p = new Pawn('b',true);
            p.addMouseListener(new MouseHandeling());// movable pawns
            sqrs[6][i].addPiece(p);
        }
        //add Rooks to Chess Board.
        sqrs[0][7].addPiece(new Rook('w',false));
        Piece p1 = new Rook('b',true);
        p1.addMouseListener(new MouseHandeling());
        sqrs[7][7].addPiece(p1);
        sqrs[0][0].addPiece(new Rook('w',false));
        Piece p2 = new Rook('b',true);
        p2.addMouseListener(new MouseHandeling());
        sqrs[7][0].addPiece(p2);
        //add Knights to Chess Board.
        sqrs[0][6].addPiece(new Knight('w',false));
        Piece p3 = new Knight('b',true);
        p3.addMouseListener(new MouseHandeling());
        sqrs[7][6].addPiece(p3);
        sqrs[0][1].addPiece(new Knight('w',false));
        Piece p4 = new Knight('b',true);
        p4.addMouseListener(new MouseHandeling());
        sqrs[7][1].addPiece(p4);
        //add Bishops to Chess Board.
        sqrs[0][5].addPiece(new Bishop('w',false));
        Piece p5 = new Bishop('b',true);
        p5.addMouseListener(new MouseHandeling());
        sqrs[7][5].addPiece(p5);
        sqrs[0][2].addPiece(new Bishop('w',false));
        Piece p6 = new Bishop('b',true);
        p6.addMouseListener(new MouseHandeling());
        sqrs[7][2].addPiece(p6);
        //add Queens to Chess Board.
        sqrs[0][4].addPiece(new Queen('w',false));
        Piece p7 = new Queen('b',true);
        p7.addMouseListener(new MouseHandeling());
        sqrs[7][4].addPiece(p7);
        //add Kings to Chess Board.
        sqrs[0][3].addPiece(new King('w',false));
        Piece p8 = new King('b',true);
        p8.addMouseListener(new MouseHandeling());
        sqrs[7][3].addPiece(p8);
        
        return t;
        //Dimiourgi JFrame kai tou prostheti to Chess board se auto me to Menu.
//        MenuFrame menuFrame = new MenuFrame();
//        menuFrame.setSize(800, 700);
//        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        menuFrame.setResizable(false);
//        menuFrame.add(t);
//        menuFrame.setVisible(true);
//        sqrs[1][0].removePiece();
//        sqrs[2][0].addPiece(new Piece("/home/dimitris/CSD/3rd_Semister/Java/ProjectForJava/branches/Kevins_Branch/Chess/src/chess/Brook.png"));
    }
}